package com.first.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.context.BaseContext;
import com.first.pojo.dto.UserPlacePageQueryDTO;
import com.first.pojo.entity.UserPlace;
import com.first.server.mapper.PlaceMapper;
import com.first.server.mapper.UserPlaceMapper;
import com.first.server.service.PlaceService;
import com.first.server.service.UserPlaceService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserPlaceServiceImpl implements UserPlaceService {

    @Autowired
    PlaceMapper placeMapper;
    @Autowired
    UserPlaceMapper userPlaceMapper;

    @Override
    public void putUserPlace(List<Integer> userIds) {
        List<UserPlace> userPlaceList = new ArrayList<>();
        for (Integer userId : userIds) {
            //该成员已加入基地，不再计入
            UserPlace  up=userPlaceMapper.selectOne(new QueryWrapper<UserPlace>().lambda()
                        .eq(UserPlace::getUserId, userId)
                        .eq(UserPlace::getPlaceId,BaseContext.getCurrentPlaceId()));
            if(up != null) continue;
            //未加入该基地的成员加入该基地
            UserPlace userPlace = UserPlace.builder()
                    .placeId(Math.toIntExact(BaseContext.getCurrentPlaceId()))
                    .userId(userId)
                    .adminId(Math.toIntExact(BaseContext.getCurrentId()))
                    .build();
            userPlaceList.add(userPlace);
        }
        userPlaceMapper.insertList(userPlaceList);
    }
}
